package cn.z2huo.knowledge.concurrency.thinkinginjava.section_21_3.syncCodeBlock;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

abstract class BasePairManager {

    AtomicInteger checkCounter = new AtomicInteger(0);

    protected Pair pair = new Pair();

    private final List<Pair> storage = Collections.synchronizedList(new ArrayList<>());

    public synchronized Pair getPair() {
        // 复制对象，保证原对象是安全的
        return new Pair(pair.getX(), pair.getY());
    }

    protected void store(Pair pair) {
        storage.add(pair);
        // 睡眠，假设该方法中的操作是一个费时的操作
        try {
            TimeUnit.MILLISECONDS.sleep(50);
        } catch (InterruptedException e) {
        }
    }

    public abstract void increment();

}
